package com.mycompany.app.dao.ibatis;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.mycompany.app.dao.PersonDao;
import com.mycompany.app.model.Person;

public class PersonDAOIBatis extends SqlMapClientDaoSupport implements
        PersonDao {

    public boolean exists(Long id) {

        return this.get(id) != null;
    }

    public Person get(Long id) {

        return (Person) getSqlMapClientTemplate().queryForObject(
                "Person.getPersonById", id);

    }

    @SuppressWarnings("unchecked")
    public List<Person> getAll() {

        return (List<Person>) getSqlMapClientTemplate().queryForList(
                "Person.getAllPerson");
    }

    public void remove(Long id) {

        getSqlMapClientTemplate().delete("Person.removePersonById", id);
    }

    public Person save(Person object) {

        if (object.getId() != null) {
            getSqlMapClientTemplate().update("Person.updatePerson", object);
        } else {
            getSqlMapClientTemplate().insert("Person.insertPerson", object);
        }
        return object;
    }
}
